﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace DOM_X10
{
    class Lexer
    {
        private String reader;
        private int index;
        private Tokken item;

        public Tokken Item
        {
            get { return item; }
            set { item = value; }
        }

        public Lexer(String Input)
        {
            reader = Input;
            index = 0;
        }

        public void Analize()
        {
            item = new Tokken(); 

            bool fin = false;
            bool num = false;
            while (reader.Length != index && !fin)
            {
                char caracter = reader[index];
                
                if (!num && Char.IsLetter(caracter))
                {
                    item.Value += reader[index];
                    index++;
                }
                else if (item.Value.Length == 0 && Char.IsDigit(caracter))
                {
                    item.Value += reader[index];
                    num = true;
                    index++;
                }
                else if (item.Value.Length == 1 && !Char.IsDigit(item.Value, 0) && Char.IsDigit(caracter))
                {
                    item.Value += reader[index];
                    num = true;
                    index++;
                }
                else if (item.Value.Length != 0 && Char.IsDigit(item.Value, item.Value.Length - 1) && Char.IsDigit(caracter))
                {
                    item.Value += reader[index];
                    num = true;
                    index++;
                }
                else
                {
                    fin = true;
                }
            }
            if (item.Value != "") Clasifica();
        }

        private void Clasifica()
        {
            Console.WriteLine("value: " + item.Value);
            bool funcionEncontrada = false;
            for (int i = 0; i < item.Funcion.Length && !funcionEncontrada; i++)
            {
                if (item.Funcion[i].ToUpper() == item.Value.ToUpper())
                {
                    Console.WriteLine("Funcion");
                    item.Type = 1;
                    item.Subtype = i;
                    item.Translate = (string)item.subtypes[i];
                    funcionEncontrada = true;
                }
            }

            if (!funcionEncontrada)
            {
                Console.WriteLine("NOFuncion");
                bool error = true;
                char ax = item.Value[0];
                
                if (Char.IsLetter(ax))
                {
                    if (item.Value.Length == 3 || item.Value.Length == 2)
                    {
                        if (ax >= 'A' && ax <= 'P')
                        {
                            item.Type = 0;
                            item.Subtype = ax - 'A'+1;
                            item.Translate = (string)item.codes[item.Subtype];
                            string devicecode = "";
                            devicecode += item.Value.Substring(1);
                            int x = int.Parse(devicecode);
                            if (x <= 16)
                            {
                                item.Translate += item.codes[x].ToString();
                                error = false;
                            }
                        }
                    }
                }
                else
                {
                    int result;
                    if (int.TryParse(item.Value, out result))
                    {
                        if (result < 23)
                        {
                            item.Type = 2;
                            item.Subtype = result;
                            item.Translate = GetIntBinaryString(result);
                            error = false;
                        }
                    }
                }
                if (error)
                {
                    item.Type = -1;
                }
            }
        }

        private string GetIntBinaryString(int n)
        {
            char[] b = new char[5];
            int pos = 4;
            int i = 0;

            while (i < 5)
            {
                if ((n & (1 << i)) != 0)
                {
                    b[pos] = '1';
                }
                else
                {
                    b[pos] = '0';
                }
                pos--;
                i++;
            }
            return new string(b);
        }
    }
}
